import type {
  IAuthorityUtils,
  IAuthorityType,
} from '../interface/IAuthorityUtils.ts';

/**
 * 位运算实现权限相关
 * * {object} authorityType - 权限类型
 * * {function} permissionSwitching - 权限的切换
 * * {function} permissionJudgment - 权限的判断
 * */
class AuthorityUtils implements IAuthorityUtils {
  public authorityType: IAuthorityType = {
    READ: 0b0001, // 0001 读
    WRITE: 0b0010, // 0010 写
    UPDATE: 0b0100, // 0100 更新
    DEL: 0b1000, // 1000 删除
  };

  constructor() {}

  // 权限组合
  // use-authority.authorityType.READ | use-authority.authorityType.DEL

  /** 权限的切换 */
  public permissionSwitching(author: number, type: number): number {
    return author ^ type;
  }

  /** 权限的判断 */
  public permissionJudgment(author: number, type: number): boolean {
    // author & type ? true : false
    return !!(author & type);
  }
}

const authorityUtils = new AuthorityUtils();
export default authorityUtils;
